package com.chenfengyao.gamereversi.ai

import com.chenfengyao.gamereversi.common.CELL_COUNT
import com.chenfengyao.gamereversi.core.ChessPiece

/**
 * Legacy strategy restored from git (commit eacc0ef), matching the original ComputerPlayer.
 * - Static weight table (weightArrayBasic)
 * - Adjust weights around owned corners (+10)
 * - Pick from all legal moves those with maximum weight; break ties randomly
 */
object LegacyStrategy {

    // 8个方向
    private val directions = arrayOf(
        intArrayOf(-1, -1), intArrayOf(-1, 0), intArrayOf(-1, 1),
        intArrayOf(0, -1), intArrayOf(0, 1),
        intArrayOf(1, -1), intArrayOf(1, 0), intArrayOf(1, 1)
    )

    // 棋盘权重（来自 eacc0ef 提交）
    private val weightArrayBasic = arrayOf(
        intArrayOf(100, -10, 8, 6, 6, 8, -10, 100),
        intArrayOf(-10, -20, 1, 2, 2, 1, -20, -10),
        intArrayOf(8, 1, 3, 4, 4, 3, 1, 8),
        intArrayOf(6, 2, 4, 0, 0, 4, 2, 6),
        intArrayOf(6, 2, 4, 0, 0, 4, 2, 6),
        intArrayOf(8, 1, 3, 4, 4, 3, 1, 8),
        intArrayOf(-10, -20, 1, 2, 2, 1, -20, -10),
        intArrayOf(100, -10, 8, 6, 6, 8, -10, 100)
    )

    fun nextMove(board: Array<Array<ChessPiece>>, player: ChessPiece): Pair<Int, Int> {
        val empty = -1 to -1
        if (player == ChessPiece.NONE) return empty

        val posList = getNextPosList(board, player)
        if (posList.isEmpty()) return empty

        val weight = adjustmentWeight(board, player)
        var maxWeight = Int.MIN_VALUE
        val bestMoves = ArrayList<Pair<Int, Int>>()
        for ((x, y) in posList) {
            val w = weight[x][y]
            if (w > maxWeight) {
                maxWeight = w
                bestMoves.clear()
                bestMoves.add(x to y)
            } else if (w == maxWeight) {
                bestMoves.add(x to y)
            }
        }
        return if (bestMoves.isNotEmpty()) bestMoves.random() else empty
    }

    private fun getNextPosList(board: Array<Array<ChessPiece>>, player: ChessPiece): List<Pair<Int, Int>> {
        val posList = ArrayList<Pair<Int, Int>>()
        for (i in 0 until CELL_COUNT) for (j in 0 until CELL_COUNT) {
            if (canPlace(board, i, j, player)) posList.add(i to j)
        }
        return posList
    }

    private fun adjustmentWeight(board: Array<Array<ChessPiece>>, player: ChessPiece): Array<IntArray> {
        val w = Array(CELL_COUNT) { i -> IntArray(CELL_COUNT) { j -> weightArrayBasic[i][j] } }
        // 角相邻三格 +10（当角已被我方占领）
        if (board[0][0] == player) { w[0][1] += 10; w[1][0] += 10; w[1][1] += 10 }
        if (board[0][CELL_COUNT - 1] == player) { w[0][CELL_COUNT - 2] += 10; w[1][CELL_COUNT - 1] += 10; w[1][CELL_COUNT - 2] += 10 }
        if (board[CELL_COUNT - 1][0] == player) { w[CELL_COUNT - 1][1] += 10; w[CELL_COUNT - 2][0] += 10; w[CELL_COUNT - 2][1] += 10 }
        if (board[CELL_COUNT - 1][CELL_COUNT - 1] == player) { w[CELL_COUNT - 1][CELL_COUNT - 2] += 10; w[CELL_COUNT - 2][CELL_COUNT - 1] += 10; w[CELL_COUNT - 2][CELL_COUNT - 2] += 10 }
        return w
    }

    private fun canPlace(board: Array<Array<ChessPiece>>, x: Int, y: Int, targetPlayer: ChessPiece): Boolean {
        if (board[x][y] != ChessPiece.NONE) return false
        val opponent = if (targetPlayer == ChessPiece.BLACK) ChessPiece.WHITE else ChessPiece.BLACK
        for (direction in directions) {
            var count = 0
            val dx = direction[0]
            val dy = direction[1]
            var tx = x + dx
            var ty = y + dy
            while (tx in 0 until CELL_COUNT && ty in 0 until CELL_COUNT) {
                if (board[tx][ty] == ChessPiece.NONE) break
                if (board[tx][ty] == opponent) {
                    count++
                } else {
                    if (count > 0) return true
                    break
                }
                tx += dx
                ty += dy
            }
        }
        return false
    }
}

